package pk.nak.responses;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import pk.nak.data.EatOutMenu;
import pk.nak.data.EatOutMenuItem;
import pk.nak.utils.XMLUtils;

public class GetEatOutMenuResponse extends BaseResponse {
	private String message;
	private EatOutMenu menu;
	
	@Override
	public void onSuccess(String response) {
		try{
			this.response = response;
			DocumentBuilderFactory dbfLoginResponse = DocumentBuilderFactory.newInstance();
	        DocumentBuilder dbLoginResponse = dbfLoginResponse.newDocumentBuilder();
	        
	        Document dom = dbLoginResponse.parse(new InputSource(new StringReader(response)));
	        NodeList nlmenu = dom.getElementsByTagName("item");
	        
	        this.menu = new EatOutMenu();
	        
	        if ( nlmenu != null && nlmenu.getLength() > 0 ){
	        	 for ( int i = 0 ; i < nlmenu.getLength(); i ++){
	        		Element eleDish = (Element)nlmenu.item(i);

    				String dishid = XMLUtils.getTextValue(eleDish, "id");
    				String dishname = XMLUtils.getTextValue(eleDish, "title");
    				String dishdescription = XMLUtils.getTextValue(eleDish, "desc");
    				String dishprice = XMLUtils.getTextValue(eleDish, "price");
    				String dishfeatured = "";//XMLUtils.getTextValue(eleDish, "featured");
	        		String itemtype = XMLUtils.getTextValue(eleDish, "type");
       				this.menu.addItem(new EatOutMenuItem(dishid,dishname, itemtype, dishprice, dishdescription, dishfeatured));
	        	}
	        	/*
	        	 for ( int i = 0 ; i < nlmenu.getLength(); i ++){
	        		Element ele = (Element)nlmenu.item(i);

	        		NodeList nldish = ele.getElementsByTagName("dish");
	        		String itemtype = ele.getAttribute("cat_name");
	        		
	        		if ( nldish != null && nldish.getLength() > 0 ){
	        			for ( int j = 0 ; j < nldish.getLength() ; j ++ ){
	        				Element eleDish = (Element)nldish.item(j);
	        				String dishname = XMLUtils.getTextValue(eleDish, "dname");
	        				String dishdescription = XMLUtils.getTextValue(eleDish, "ddesc");
	        				String dishprice = XMLUtils.getTextValue(eleDish, "dprice");
	        				String dishfeatured = XMLUtils.getTextValue(eleDish, "featured");
	        				
	        				this.menu.addItem(new EatOutMenuItem(dishname, itemtype, dishprice, dishdescription, dishfeatured));
	        			}
	        		}
	        	}
	        	*/
	        }
		}catch(Exception ex){
			
		}
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public EatOutMenu Menu(){
		return this.menu;
	}
}
